// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Lu Rong
// Date   : 2005-03-18

#include "stdafx.h"
#include "trenchlist.h"
#include ".\trenchlist.h"

namespace JoyTrenchElem
{

// class CJoyTrenchListField
//##ModelId=44C4B5DA02A3
void CJoyTrenchListField::CM_Draw(Graphics &graph)
{
	int w = m_area.Width();
	int h = m_area.Height();

	StringFormat sf;
	sf.SetAlignment(StringAlignmentCenter);
    sf.SetLineAlignment(StringAlignmentCenter);

    SolidBrush txBrush(m_textColor);

    FontFamily fam(m_textFace);
    Font font(&fam, (REAL)m_textSize, FontStyleRegular, UnitPixel);

    graph.DrawString(
        m_title, m_title.GetLength(),
        &font,
        RectF((REAL)0, (REAL)0, (REAL)w, (REAL)h),
        &sf, &txBrush);

	SolidBrush frameBrush(0xFFFFFFFF);
	graph.FillRectangle(&frameBrush, 0, 0, w, 2);
	graph.FillRectangle(&frameBrush, w - 2, 0, 2, h);
	graph.FillRectangle(&frameBrush, 0, h - 2, w, 2);

	if (m_area.left <= 0)
		graph.FillRectangle(&frameBrush, 0, 0, 2, h);
}

// class CJoyTrenchListItem
//##ModelId=44C4B5DA02C1
void CJoyTrenchListItem::CM_Draw(Graphics &graph)
{
	//int w = m_area.Width();
	int h = m_area.Height();

	CJoyTrenchList * trenchList = GetTrenchList();

	StringFormat sf;
	sf.SetAlignment(StringAlignmentCenter);
    sf.SetLineAlignment(StringAlignmentCenter);
    sf.SetTrimming(StringTrimmingEllipsisCharacter);
    sf.SetFormatFlags(StringFormatFlagsNoWrap);
    FontFamily fam(trenchList->m_textFace);
    Font font(&fam, (REAL)trenchList->m_textSize, FontStyleRegular, UnitPixel);

	// draw nickname
	{
		SolidBrush nameBrush(trenchList->m_nicknameField->m_itemTextColor);
		int nw = trenchList->m_nicknameField->GetArea().Width();
		int nx = trenchList->m_nicknameField->GetArea().left;
		graph.DrawString(m_nickName, m_nickName.GetLength(), &font, 
			RectF((REAL)nx, (REAL)0, (REAL)nw, (REAL)h), &sf, &nameBrush);
	}

	// draw killcount
	{
		SolidBrush killBrush(trenchList->m_killcountField->m_itemTextColor);
		CStringW killCount;
		killCount.Format(L"%d", m_killCount);
		int kw = trenchList->m_killcountField->GetArea().Width();
		int kx = trenchList->m_killcountField->GetArea().left;
		graph.DrawString(killCount, killCount.GetLength(), &font, 
			RectF((REAL)kx, (REAL)0, (REAL)kw, (REAL)h), &sf, &killBrush);
	}

	// draw bekillcount
	{
		SolidBrush bekillBrush(trenchList->m_bekillcountField->m_itemTextColor);
		CStringW bekillCount;
		bekillCount.Format(L"%d", m_bekillCount);
		int bw = trenchList->m_bekillcountField->GetArea().Width();
		int bx = trenchList->m_bekillcountField->GetArea().left;
		graph.DrawString(bekillCount, bekillCount.GetLength(), &font, 
			RectF((REAL)bx, (REAL)0, (REAL)bw, (REAL)h), &sf, &bekillBrush);
	}
}

//##ModelId=44C4B5DA0301
CJoyTrenchListItemPanel::CJoyTrenchListItemPanel() 
{ 
	m_listView  = NULL;
	m_align = AT_Client;
	m_name = "item_panel";
}

//##ModelId=44C4B5DA02F5
void CJoyTrenchListItemPanel::CM_Draw(Graphics& graph)
{
	__super::CM_Draw(graph);

	int w = m_area.Width();
	int h = m_area.Height();

	CJoyTrenchList * trenchList = GetTrenchList();

	int ih = trenchList->m_itemHeight;

	SolidBrush backColor2(m_backColor2); 
	SolidBrush frameColor(m_backColor);
	graph.FillRectangle(&backColor2, 0, 0, w, ih);
	graph.FillRectangle(&backColor2, 0, ih * 2, w, ih);

	POSITION pos = trenchList->m_fields.GetHeadPosition();
	while (pos != NULL)
	{
		CJoyListViewField *p = trenchList->m_fields.GetNext(pos);
		int iw = p->GetArea().Width();
		int ix = p->GetArea().left;
		graph.FillRectangle(&frameColor, ix + iw - 2, 0, 2, h);
		if (ix <= 0)
			graph.FillRectangle(&frameColor, 0, 0, 2, h);
	}
}

//##ModelId=44C4B5DA02FE
void CJoyTrenchListItemPanel::CM_LoadSkin(CJoyXmlElement *pSkinElem)
{
	__super::CM_LoadSkin(pSkinElem);

	m_backColor2.SetValue(pSkinElem->ReadIntX("back_color2", m_backColor2.GetValue()));
}

// class CJoyTrenchList
IMPLEMENT_DYNAMIC(CJoyTrenchList, CJoyListView)

//##ModelId=44C4B5DA033E
CJoyTrenchList::CJoyTrenchList()
{
	m_name = "trench_list";
}

//##ModelId=44C4B5DA033F
CJoyTrenchList::~CJoyTrenchList()
{}

BEGIN_MESSAGE_MAP(CJoyTrenchList, CJoyListView)
	ON_WM_CREATE()
END_MESSAGE_MAP()

//##ModelId=44C4B5DA031D
void CJoyTrenchList::CM_LoadSkin(CJoyXmlElement *pSkinElem)
{
	__super::CM_LoadSkin(pSkinElem);
}

//##ModelId=44C4B5DA0320
CJoyListViewPanel * CJoyTrenchList::CreatePanel()
{
	return new CJoyTrenchListItemPanel();
}

template<typename FieldType>
void CJoyTrenchList::AddField_T(FieldType*& f, PCSTR name)
{
	f = new FieldType();
	f->m_name = name;
	AddFieldTail(f);
}

//##ModelId=44C4B5DA0341
int CJoyTrenchList::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CJoyListView::OnCreate(lpCreateStruct) == -1)
		return -1;

	AddField_T(m_nicknameField, "nickname");
	AddField_T(m_killcountField, "killcount");
	AddField_T(m_bekillcountField, "bekillcount");

	return 0;
}

}; // namespace JoyTrenchElem
